package com.uinnova.product.eam.web.config.mvc;

import com.binary.framework.util.ControllerUtils;
import com.binary.json.JSON;
import com.uinnova.product.eam.comm.model.CVcBaseConfig;
import com.uinnova.product.eam.comm.model.VcBaseConfig;
import com.uinnova.product.eam.service.BaseConfigService;
import com.uinnova.product.eam.web.config.peer.BaseConfigPeer;
import com.uinnova.product.eam.web.diagram.peer.SystemDiagramPeer;
import com.uinnova.product.eam.web.util.SqlInjectUtil;
import com.uinnova.product.vmdb.comm.bean.QueryListCondition;
import com.uinnova.product.vmdb.comm.doc.annotation.ModDesc;
import com.uinnova.product.vmdb.comm.doc.annotation.MvcDesc;
import com.uinnova.product.vmdb.comm.model.monitor.CMonSysSeverity;
import com.uinnova.product.vmdb.comm.model.monitor.MonSysSeverity;
import com.uinnova.product.vmdb.comm.util.RestTypeUtil;
import com.uinnova.product.vmdb.comm.util.SystemUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;


@Controller
@RequestMapping("/config")
@MvcDesc(author = "wangchuanping", desc = "提供配置相关接口")
public class BaseConfigMvc {

	@Autowired
	private BaseConfigPeer configPeer;

	@Resource
	private BaseConfigService baseConfigService;

	@Resource
	private SystemDiagramPeer systemDiagramPeer;


	@RequestMapping("/queryConfigList")
	@ModDesc(desc = "查询配置列表", pDesc = "查询条件", pType = CVcBaseConfig.class, rDesc = "配置信息列表", rType = List.class,
			rcType = VcBaseConfig.class)
	public void queryConfigList(HttpServletRequest request, HttpServletResponse response, @RequestBody String body) {
		QueryListCondition<CVcBaseConfig> condition = RestTypeUtil.toListCondition(body, CVcBaseConfig.class);
		List<VcBaseConfig> result = baseConfigService.queryBaseConfigList(condition.getCdt());
		ControllerUtils.returnJson(request, response, result);
	}

	@RequestMapping("/saveOrUpdateConfig")
	@ModDesc(desc = "保存或者更新配置", pDesc = "配置信息", pType = VcBaseConfig.class, rDesc = "保存id", rType = Long.class)
	public void saveOrUpdateConfig(HttpServletRequest request, HttpServletResponse response,
								   @RequestBody String body) {
		VcBaseConfig record = JSON.toObject(body, VcBaseConfig.class);
		if (record != null && record.getCfgCode() != null) {
			SqlInjectUtil.hasSpecialStr(record.getCfgCode());
		}

		Long result = baseConfigService.saveOrUpdateBaseConfig(record);
		ControllerUtils.returnJson(request, response, result);
	}
	
	@RequestMapping("/saveOrUpdateConfigBatch")
	@ModDesc(desc = "批量保存或者更新配置", pDesc = "配置信息",pType=List.class,pcType=VcBaseConfig.class, rDesc = "保存id", rType = Integer.class)
	public void saveOrUpdateConfigBatch(HttpServletRequest request, HttpServletResponse response, @RequestBody String body){
		List<VcBaseConfig> records = JSON.toList(body,VcBaseConfig.class);
		Integer result = configPeer.saveOrUpdateConfigBatch(records);
		ControllerUtils.returnJson(request, response, result);
	}

	@PostMapping("/saveOrUpdateBaseConfigBatch")
	public void saveOrUpdateBaseConfigBatch(HttpServletRequest request, HttpServletResponse response, @RequestBody List<VcBaseConfig> baseConfigList){
		Long result = baseConfigService.saveOrUpdateBaseConfigBatch(baseConfigList);
		ControllerUtils.returnJson(request, response, result);
	}
	
	@RequestMapping("/saveAppWallOrderConfig")
	@ModDesc(desc = "保存应用墙的应用顺序", pDesc = "内容信息",pType=String.class, rDesc = "保存文件的路径", rType = String.class)
	public void saveAppWallOrderConfig(HttpServletRequest request, HttpServletResponse response, @RequestBody String body){
//		String record = JSON.toObject(body,String.class);
		String token = SystemUtil.getToken(request);
		String result = configPeer.saveAppWallOrderConfig(body,token);
		ControllerUtils.returnJson(request, response, result);
	}
	@RequestMapping("/queryAppWallOrderConfig")
	@ModDesc(desc = "查询应用墙的应用顺序信息", pDesc = "", rDesc = "应用墙的应用顺序信息", rType = String.class)
	public void queryAppWallOrderConfig(HttpServletRequest request, HttpServletResponse response){
		String token = SystemUtil.getToken(request);
		String result = configPeer.queryAppWallOrderConfig(token);
		ControllerUtils.returnJson(request, response, result);
	}
	
	@RequestMapping("/saveOrUpdateConfigFile")
	@ModDesc(desc = "保存应用墙的应用顺序", pDesc = "内容信息",pType=String.class, rDesc = "保存文件的路径", rType = String.class)
	public void saveOrUpdateConfigFile(HttpServletRequest request, HttpServletResponse response, @RequestBody String body){
		VcBaseConfig record = JSON.toObject(body,VcBaseConfig.class);
		if (record != null && record.getCfgCode() != null) {
			SqlInjectUtil.hasSpecialStr(record.getCfgCode());
		}
		String token = SystemUtil.getToken(request);
		String result = configPeer.saveOrUpdateConfigFile(record.getCfgCode(),record.getCfgContent(),token);
		ControllerUtils.returnJson(request, response, result);
	}
	@RequestMapping("/queryConfigFile")
	@ModDesc(desc = "查询应用墙的应用顺序信息", pDesc = "", rDesc = "应用墙的应用顺序信息", rType = String.class)
	public void queryConfigFile(HttpServletRequest request, HttpServletResponse response ,@RequestBody String body){
		String token = SystemUtil.getToken(request);
		String cfgCode = JSON.toObject(body,String.class);
		String result = configPeer.queryConfigFile(cfgCode,token);
		ControllerUtils.returnJson(request, response, result);
	}

	@RequestMapping("/removeConfigByCdt")
	@ModDesc(desc = "条件删除配置信息", pDesc = "删除目标的条件",pType=CVcBaseConfig.class, rDesc = "", rType = Integer.class)
	public void removeConfigByCdt(HttpServletRequest request, HttpServletResponse response, @RequestBody String body){
		CVcBaseConfig cdt = JSON.toObject(body,CVcBaseConfig.class);
		Integer result = configPeer.removeConfigByCdt(cdt);
		ControllerUtils.returnJson(request, response, result);
	}
	
}
